<template>
    <div id="member" class="wrapper-fixed">
        <div class="action-bar-wrapper">
            <el-col :span="24" class="action-bar">
                <el-button type="primary" size="medium" @click="addStaffVisible=true">添加</el-button>
            </el-col>
        </div>

        <!-- 列表 -->
        <el-table
            :data="tableData"
            style="width: 100%">
            <el-table-column
                prop="realname"
                label="管理员"
                width="180">
            </el-table-column>
            <el-table-column
                prop="phone"
                label="手机"
                width="120"
                >
            </el-table-column>
            <el-table-column
                prop="created_at"
                label="创建时间"
                width="160"
                >
            </el-table-column>
            <el-table-column label="操作" width='143'>
                <template slot-scope="scope">
                  <el-button type="danger" size="mini" @click="deleteStaff(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-col :span="24" class="footer-bar">
			<div class="page-code">
				<el-pagination
					background
					:page-size="16"
					layout="prev, pager, next"
					:total="totalPage"
					@current-change="pageChange"
						>
				</el-pagination>
			</div>
    </el-col>
    <!-- 列表end -->

        <!-- 添加弹窗 -->
		<el-dialog title="添加" :visible.sync="addStaffVisible" @close="initData" width="30%">
            <el-form>
                <el-form-item label="添加" label-width="50px">
                    <el-select
                        v-model="executor"
                        filterable
                        remote
                        :loading="loading"
                        placeholder="请输入用户"
                        @change="getExecutor"
                        :remote-method="searchName"
                        size="medium"
                            >
                        <div v-if="vipList.length!=0">
                            <el-option
                            v-for="(item) in vipList"
                            :key="item.id"
                            :label="item.realname?item.realname:item.nickname"
                            :value="item.user_id">
                            <template >
                                <div style="display:flex"><img :src="item.photo_url" style="width:30px;border-radius:50%;height:30px;object-fit:cover" alt="">&nbsp;&nbsp;&nbsp;{{item.realname?item.realname:item.nickname}}&nbsp;&nbsp;&nbsp;{{item.phone}} </div>
                            </template>
                        </el-option>
                        </div>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="setCircleAdmin">添加</el-button>
			</div>
		</el-dialog>
        <!-- 添加弹窗end -->



    </div>
</template>
<script>
export default {
    name:'member',
    data() {
        return {
            addStaffVisible:false,//弹窗状态
            searchText:'',
            userAuthId:'',//选中的用户id


            vipList:[],
            executor:'',
            loading:false,
            
            tableData: [{}],
            page:1,
            totalPage:0    
        }
    },
    watch: {

    },
    methods: {
        search(){
            this.page=1;
            this.getUnitAdminList()
        },
        async getUnitAdminList(){
            //获取会员列表
            let params={
                page:this.page,
                limit:16,
                id:this.id
            }
            let res=await this.$api.getUnitAdminList(
                params);
            if(res.data.level=='success'){
                this.tableData=res.data.data;
                this.totalPage=res.data.page_info.total;
            }
        },
        showPersodDetail(id){
            this.$router.push({name:'Customer_detail',query:{id:id}})
        },
        pageChange(e){
			//翻页
			this.page=e;
			this.getUnitAdminList();
        },
        async deleteStaff(id){
            //删除
            let judge=null;
            let tit='删除该管理员?';
            try {
              judge=  await this.$confirm(tit, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
                })
            } catch (error) {
                judge='cancel'
            }
            if(judge=='confirm'){
                this.delUnitEmployee(id)
            }
            console.log(judge)
        },
        async delUnitEmployee(id){
            let res =await this.$api.delUnitEmployee({id:id})
            if(res.data.level=='success'){
                this.$message({
                    type:'success',
                    message:res.data.message
                })
                this.getUnitAdminList();
            }else{
                this.$message({
                    type:'error',
                    message:res.data.message
                })
            }
        },
        getExecutor(e){
            this.executor=e;
		    },
		async searchName(searchText){
			//搜索用户名
			this.loading=true;
            let params={
                page:1,
                limit:999,
                keyword: searchText
            }
            let res=await this.$api.getUserList(params);
			if(res.data.level=='success'){
                let arr=[]
                res.data.data.forEach(item=>{
                    if(item.user_info){
                        item.user_info.phone=item.phone
                        arr.push(item.user_info) 
                    }
                });
                this.vipList=arr
			}
			this.loading=false
        }, 
        async setCircleAdmin(){
            let params={
                user_id:this.executor,
                unit_id:this.id
            }
            let res=await this.$api.addUnitAdmin(params);
            if(res.data.level=='success'){
                this.$message({
                    type:'success',
                    message:res.data.message
                })
                this.addStaffVisible = false
                this.getUnitAdminList()
            }else{
                this.$message({
                    type:'error',
                    message:res.data.message
                })
            }
        },
        initData(){

        }

    },
    mounted() {
        this.id=this.$route.query.id
        this.getUnitAdminList()
    },
}
</script>
<style lang="scss" scoped>
    .screen{
          margin-top: -10px !important;
      }
      .action-bar-wrapper{
          position: sticky;
            z-index: 1;
            top:-10px;
      }
    .action-bar{
			background-color: #f2f2f2;
            
			height: 60px;
			margin: 10px 0px;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			padding-left: 20px;
			.action-bar-input{
				width: 250px;
				margin-right: 14px;
      }
      .task-form-text{
				color: #666666;
			}
		}
		.el-table-style{
			width: 100%;
			border: 1px solid #f2f2f2;
		}
		.footer-bar{
			display: flex;
			justify-content: space-between;
			align-items: center;
			background: #f2f2f2;
			padding: 8px;
			margin: 10px 0px;
			box-sizing: border-box;
		}
		.dialog-footer{
			text-align: center;
		}
		.executorList{
			display: flex;
			flex-wrap: wrap;
			div{
				margin:10px;
				border:1px solid #eee;
				border-radius: 5px;
				padding:5px 10px;
			}
		}
</style>